﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Mang24k99.Model.COMM;
using Mang24k99.Business.COMM;
using Wuqi.Webdiyer;


namespace Mang24k99.Web.COMM
{
    public partial class KtCommBaseInfo : System.Web.UI.Page
    {
        string _AspNetName = "CustomInfoHTML";

        public string AspNetCustomInfoHTML
        {
            get
            {
                return Common.ViewStateHelper.GetString(this.ViewState, _AspNetName);
            }
            set
            {
                Common.ViewStateHelper.SetViewState(this.ViewState, value, _AspNetName);
            }
        }
       protected void Page_Load(object sender, EventArgs e)
        {
            this.btnCancel.Attributes.Add("onclick", "return Cancel()");
            if (!IsPostBack)
            {
                this.AspNetCustomInfoHTML = this.AspNetPager1.CustomInfoHTML;
                this.Panel1.Visible = false;
                BindAspNetPager();
                BinDate();
            }
        }
        //
        /// <summary>
        /// 新增修改一条记录
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {
           
           
            //如果是新增操作
            if (Session["Action"].ToString() == "Insert")
            {
                if (CodeValidate()==false)
                {
                    return;
                }
                if (NameValidate()==false)
                {
                    return;
                }
                if (ValueValidate()==false)
                {
                    return;
                }
                MKtCommBaseInfo commbase = new MKtCommBaseInfo();
                commbase.FACTOR_CODE = txtCode.Text.Trim();
                commbase.FACTOR_NAME = txtName.Text.Trim();
                commbase.FACTOR_VALUE = txtValue.Text.Trim();
                commbase.COMMENTS = txtRemark.Text.Trim();
                commbase.IS_VALIDATE = Int32.Parse(ddlIsValidate.SelectedValue);
                commbase.INPUT_TIME = DateTime.Now;
                Boolean result = BKtCommBaseInfo.Save(commbase);
                if (result == true)
                {
                    string strMsg = "新增成功！";
                    Common.MessageBox.Show(this.Page, strMsg);
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    ClearPage();
                    BindAspNetPager();
                    BinDate();
                }
                else
                {
                    string strMsg = "新增失败！";
                    Common.MessageBox.Show(this.Page, strMsg);
                }
            }
            //如果是修改操作
            if (Session["Action"].ToString() == "Update")
            {
                if (txtCode.Text.Trim()!=Session["Code"].ToString())
                {
                   if (CodeValidate()==false)
                    {
                        return;
                    }
                }
                if (txtName.Text.Trim()!=Session["Name"].ToString())
                {  
                      if (NameValidate()==false)
                        {
                            return;
                        }
                }
                if (ValueValidate()==false)
                {
                    return;
                }
                MKtCommBaseInfo commbase = new MKtCommBaseInfo();
                commbase.FACTOR_CODE = txtCode.Text.Trim();
                commbase.FACTOR_NAME = txtName.Text.Trim();
                commbase.FACTOR_VALUE = txtValue.Text.Trim();
                commbase.COMMENTS = txtRemark.Text.Trim();
                commbase.IS_VALIDATE = Int32.Parse(ddlIsValidate.SelectedValue);
                commbase.MODIFY_TIME = DateTime.Now;
                commbase.ID = Int32.Parse(Session["ID"].ToString());
                Boolean result = BKtCommBaseInfo.Update(commbase);
                if (result == true)
                {
                    string strMsg = "修改成功";
                    Common.MessageBox.Show(this.Page, strMsg);
                    this.Panel1.Visible = false;
                    this.Panel2.Visible = true;
                    ClearPage();
                    BindAspNetPager();
                    BinDate();
                }
                else
                {
                    string strMsg = "修改失败！";
                    Common.MessageBox.Show(this.Page, strMsg);
                }
            }
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            if (Session["Action"]!=null)
            {
                if (Session["Action"].ToString() == "Insert")
                {
                    ClearPage();
                }
                else
                {
                    if (Session["ID"]!=null)
                    {
                        InItDate(Int32.Parse(Session["ID"].ToString()));
                    }
                }
            }
        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="type"></param>
        public void InItDate(int ID)
        {
            MKtCommBaseInfo baseinfo = MKtCommBaseInfo.Select(ID);
            this.txtCode.Text = baseinfo.FACTOR_CODE;
            this.txtName.Text = baseinfo.FACTOR_NAME;
            this.txtRemark.Text = baseinfo.COMMENTS;
            this.txtValue.Text = baseinfo.FACTOR_VALUE;
            this.ddlIsValidate.SelectedValue = baseinfo.IS_VALIDATE.ToString();
        }
        /// <summary>
        /// 绑定gridview
        /// </summary>
        public void BinDate()
        {
            AspNetPager aspNetPager = this.AspNetPager1;
            int maxRecord = aspNetPager.PageSize;
            int startRecord = aspNetPager.StartRecordIndex - 1;
            object dataSource = BKtCommBaseInfo.FindALLSliced(startRecord, maxRecord);
            this.grdList.DataSource = dataSource;
            this.grdList.DataBind();
        }
        /// <summary>
        /// 清空页面文本值
        /// </summary>
        public void ClearPage()
        {
            txtCode.Text = "";
            txtName.Text = "";
            txtRemark.Text = "";
            txtValue.Text = "";
            ddlIsValidate.SelectedValue = "1";

        }
        /// <summary>
        /// 转换是否有效字段的字符串格式
        /// </summary>
        /// <param name="isValidate"></param>
        /// <returns></returns>
        public string GetStatus(int isValidate)
        {
            if (isValidate==1)
            {
                return "使用";
            } 
            else
            {
                return "禁用";
            }
        }

        /// <summary>
        /// 选择gridview中某一行进行编辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void grdList_SelectedIndexChanged(object sender, EventArgs e)
        {
            int ind =this.grdList.SelectedIndex;
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            if (grdList.Rows[ind].Cells[2].Text!="")
            {
                this.txtCode.Text = grdList.Rows[ind].Cells[2].Text;
                Session["Code"]=grdList.Rows[ind].Cells[2].Text;
            }
            if (grdList.Rows[ind].Cells[3].Text!="")
            {
                this.txtName.Text = grdList.Rows[ind].Cells[3].Text;
                Session["Name"]=grdList.Rows[ind].Cells[3].Text;
            }
            if (grdList.Rows[ind].Cells[4].Text!="")
            {
               this.txtValue.Text = grdList.Rows[ind].Cells[4].Text;
            }

            if (grdList.Rows[ind].Cells[6].Text != "" && grdList.Rows[ind].Cells[6].Text != "&nbsp;")
            {
                this.txtRemark.Text = grdList.Rows[ind].Cells[6].Text;
            }
            GridViewRow gvr = grdList.Rows[ind];
            Label lab = (Label)gvr.FindControl("lab");
            this.ddlIsValidate.ClearSelection();
            this.ddlIsValidate.Items.FindByText(lab.Text).Selected = true;
            Session["Action"] = "Update";
            Session["ID"] = grdList.Rows[ind].Cells[1].Text;
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = true;
            this.Panel2.Visible = false;
            //this.btnSave.Text = "修改";
            Session["Action"] = "Insert";
        }
        public void grdList_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            MKtCommBaseInfo commbase = new MKtCommBaseInfo();
            string id=this.grdList.Rows[e.RowIndex].Cells[1].Text;
            commbase = (MKtCommBaseInfo)MKtCommBaseInfo.Select(Convert.ToInt32(id));
            Boolean result = BKtCommBaseInfo.Delete(commbase);
            if (result == true)
            {
                string strMsg = "删除成功！";
                Common.MessageBox.Show(this.Page, strMsg);
                BindAspNetPager();
                BinDate();
            }
            else
            {
                string strMsg = "删除失败！";
                Common.MessageBox.Show(this.Page, strMsg);
            }
        }


       //代码编码有效性验证
        public bool CodeValidate()
        {
            string strValidate = BKtCommBaseInfo.CodeValidate(this.txtCode.Text.Trim());
            if (strValidate != "success")
            {
                string strMsg = strValidate;
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }

        public bool NameValidate()
        {
            //验证代码名称有效性
            string strNameValidate = BKtCommBaseInfo.NameValidate(this.txtName.Text.Trim());
            if (strNameValidate != "success")
            {
                string strMsg = strNameValidate;
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }

        public bool ValueValidate()
        {
            //验证参数值有效性
            string strValueValidate = BKtCommBaseInfo.ValueValidate(this.txtValue.Text.Trim());
            if (strValueValidate != "success")
            {
                string strMsg = strValueValidate;
                Common.MessageBox.Show(this.Page, strMsg);
                return false;
            }
            return true;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            this.Panel1.Visible = false;
            this.Panel2.Visible = true;
            ClearPage();
        }

        /// <summary>
        /// Binds the ASP net pager.(绑定分页控件)
        /// </summary>
        private void BindAspNetPager()
        {
            AspNetPager aspNetPager = this.AspNetPager1;
            int RecordCount = BKtCommBaseInfo.GetCount();
            aspNetPager.RecordCount = RecordCount;
            aspNetPager.CustomInfoHTML = string.Format(AspNetCustomInfoHTML, aspNetPager.PageCount);
        }
        /// <summary>
        /// Handles the PageChanged event of the AspNetPager1 control.(列表改变时候)
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindAspNetPager();
            BinDate();
        }
   }
}
